<!--
  @anthor: darker,
  @description: 表格过滤，字段排序
  @date: 2020 / 07 / 28,
  @remark:
 -->
<!-- ***************** 请保持数据一致性！*****************-->
<!--
 引用示例：
 <fillter :filterList="tableHead" :visible="dialogVisible" @dialog="dialog" @callback="sycntableHead"></fillter>
 filterList：传入的表头数据
 dialogVisible：控制显隐 与@dialog关闭事件回调一起使用
 @dialog：关闭回调
 @callback：确认事件回调,传出字段过滤并排序后的数据，数据格式与传入时一致
  -->
<template>
  <div class="lump" v-if="visible">
    <div class="choice_box">
      <div class="clearfix top_tips">
        <p class="fl tips">设置列表显示字段</p>
        <p @click="closeVisible()" class="close fr">
          <i class="el-icon-close"></i>
        </p>
      </div>
      <div class="clearfix bot_bot">
        <div class="left_bot">
          <div class="bot_left_bar">
            <!-- <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox> -->
            <el-checkbox v-model="checkAll" @change="handleCheckAllChange">{{
              checkAll ? "全不选" : "全选"
            }}</el-checkbox>
          </div>
          <div class="bot_left_content">
            <el-checkbox
              v-for="(item, index) in initial"
              :disabled="item.necessity"
              v-model="item.isAbled"
              :label="item.prop"
              :key="item.prop"
              @change="checked => handleCheckedCitiesChange(checked, index)"
              >{{ item.label }}</el-checkbox
            >
            <p class="tip" v-if="JSON.stringify(initial) === '[]'">
              无数据,请核对您的参数是否有误！
            </p>
          </div>
        </div>
        <div class="right_bot">
          <div class="bot_right_bar">
            当前选定字段
            <el-tooltip placement="bottom">
              <div slot="content">
                “
                <i class="el-icon el-icon-star-on"></i
                >”为必须字段标记,无法移除！
              </div>
              <i class="el-icon el-icon-info mgl"></i>
            </el-tooltip>
          </div>
          <div class="bot_right_content">
            <!--  -->
            <draggable
              v-model="checkedFiledList"
              group="people"
              @start="onStart"
              @end="onEnd"
            >
              <transition-group>
                <div
                  class="item"
                  v-for="(item, index) in checkedFiledList"
                  :key="item.prop"
                >
                  <i class="el-icon el-icon-discount"></i>
                  <p>{{ item.label }}</p>
                  <i
                    class="el-icon el-icon-close"
                    @click="delFiled(index, item.prop)"
                    v-if="!item.necessity"
                  ></i>
                  <i v-else class="el-icon el-icon-star-on"></i>
                </div>
              </transition-group>
            </draggable>
            <p class="tip" v-if="JSON.stringify(checkedFiledList) === '[]'">
              无数据！
            </p>
            <p></p>
          </div>
        </div>
      </div>
      <div class="bot_bottom_bot">
        <el-button-group>
          <el-button size="mini" @click="closeVisible()">关闭</el-button>
          <el-button type="primary" size="mini" @click="submit()"
            >确定</el-button
          >
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import draggable from "vuedraggable";
export default {
  name: "fillter",
  props: {
    filterList: {
      type: Array,
      default: []
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      initial: [], //初始化数据
      checkAll: false,
      checkedFiled: [],
      checkedFiledList: [],
      isIndeterminate: true,
      drag: false
    };
  },
  computed: {},
  watch: {
    visible: function() {
      this.isCheckedAll();
    }
  },
  methods: {
    beforeClose() {},
    onStart() {
      this.drag = true;
    },
    onEnd() {
      this.drag = false;
    },
    handleCheckAllChange(val) {
      let that = this;
      console.log("全选");
      that.checkedFiled = [];
      that.checkedFiledList = [];
      if (val) {
        that.initial.map((item, index) => {
          item.isAbled = true;
          that.checkedFiled.push(item.prop);
          that.checkedFiledList.push(item);
        });
      } else {
        that.initial.map((item, index) => {
          if (!item.necessity) {
            item.isAbled = false;
          } else {
            that.checkedFiled.push(item.prop);
            that.checkedFiledList.push(item);
          }
        });
      }
      that.isCheckedAll();
      that.$forceUpdate();
    },
    // 判定是否全选
    isCheckedAll() {
      this.checkAll = this.checkedFiled.length === this.initial.length;
    },
    handleCheckedCitiesChange(value, index) {
      let that = this;
      if (value) {
        that.initial[index].isAbled = true;
        that.checkedFiled.push(that.initial[index].prop);
        that.checkedFiledList.push(that.initial[index]);
      } else {
        that.initial[index].isAbled = false;
        that.checkedFiledList.map((item, key) => {
          if (item.prop == that.initial[index].prop) {
            that.checkedFiledList.splice(key, 1);
            that.checkedFiled.splice(key, 1);
          }
        });
      }
      // let checkedCount = this.checkedFiled.length;
      // this.checkAll = checkedCount === this.initial.length;
      // this.isIndeterminate = checkedCount > 0 && checkedCount < this.initial.length;
      that.isCheckedAll();
    },
    delFiled(index, prop) {
      let that = this;
      let currentIndex = this.initial.findIndex(item => item.prop == prop);
      if (currentIndex != -1) {
        that.initial[currentIndex].isAbled = false;
      }
      that.checkedFiled.map((item, key) => {
        if (item == that.checkedFiledList[index].prop) {
          that.checkedFiled.splice(key, 1);
        }
      });
      that.checkedFiledList.splice(index, 1);
    },
    closeVisible() {
      this.$emit("dialog", []);
    },
    submit() {
      let that = this;
      let arr = [],
        finalData;
      that.initial.forEach(function(item, index) {
        let boolean = false;
        that.checkedFiledList.forEach(function(val, key) {
          if (item.prop === val.prop) {
            boolean = true;
            return;
          }
        });
        if (!boolean) {
          arr.push(item);
        }
        console.log(arr);
      });
      finalData = JSON.parse(JSON.stringify(that.checkedFiledList));
      finalData = finalData.concat(arr);
      finalData.map((_item, _index) => {
        _item.index = _index;
      });
      that.$emit("callback", finalData);
      that.closeVisible();
    }
  },
  created() {
    let that = this;
    let data = JSON.parse(JSON.stringify(that.filterList));
    that.initial = data;
    that.initial.map((item, index) => {
      if (item.isAbled) {
        that.checkedFiled.push(item.prop);
        that.checkedFiledList.push(item);
      }
    });
    that.isCheckedAll();
  },
  components: {
    draggable
  }
};
</script>

<style lang="less" scoped>
.lump {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
}

.mgl {
  margin-left: 4px;
}

.choice_box {
  position: absolute;
  width: 60%;
  height: 60%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #ffffff;
  border-radius: 2px;
  min-width: 850px;
  min-height: 300px;
}

.top_tips {
  margin: 0 20px;
  height: 54px;
  line-height: 54px;
  border-bottom: 1px solid rgb(233, 233, 233);
}

.top_tips .tips {
  font-size: 18px;
  font-weight: bold;
  color: #333333;
}

.top_tips i {
  font-size: 18px;
  color: #333333;
}

.bot_bot {
  height: calc(100% - 110px);
  box-sizing: border-box;
  margin: 0 20px;
  border-bottom: 1px solid rgb(233, 233, 233);
}

.bot_bot > div {
  height: 100%;
}

.tree_box {
  box-sizing: border-box;
  width: 24%;
  border-right: 1px solid rgb(233, 233, 233);
}

// .table_box {
//   box-sizing: border-box;
//   padding: 20px;
//   width: 76%;
//   overflow-y: scroll;
// }

// .el-table__body-wrapper {
//   min-height: 80% !important;
// }

.search_box {
  margin-bottom: 16px;
}

// .el-pagination {
//   margin: 20px 10px 10px 10px;
//   text-align: right;
// }

.close {
  width: 34px;
  height: 34px;
  margin: 10px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close .el-icon-close {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.el-icon-close:hover {
  color: red;
  background-color: #f0f0f0;
  border-radius: 50%;
}

.left_bot {
  width: calc(100% - 220px);
  height: 100%;
  float: left;
}

.bot_left_bar {
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(233, 233, 233);
  box-sizing: border-box;
}

.bot_left_content {
  padding: 10px 0;
  width: 100%;
  height: calc(100% - 40px);
  overflow: auto;
  box-sizing: border-box;
}

.bot_left_content .el-checkbox__label {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bot_left_content .el-checkbox__input {
  float: left;
}

.bot_left_bar .el-checkbox {
  margin: 0;
}

.el-checkbox {
  width: 20%;
  margin-bottom: 10px;
  margin-right: 0 !important;
}

.right_bot {
  width: 220px;
  height: 100%;
  float: left;
  border-left: 1px solid rgb(233, 233, 233);
  box-sizing: border-box;
  overflow-y: auto;
}

.bot_right_bar {
  box-sizing: border-box;
  padding: 0 12px;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(233, 233, 233);
}

.bot_right_content {
  overflow-y: auto;
  display: block;
  width: 100%;
  height: calc(100% - 40px);
}

.item {
  width: 100%;
  height: 28px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  box-sizing: border-box;
  cursor: move;
}

.item p,
.item .el-icon {
  float: left;
}

.item .el-icon {
  width: 15px;
  margin-right: 10px;
  cursor: pointer;
}

.item p {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  flex: 1;
}

.item:active {
  border: 1px solid #f4f4f5;
  -moz-box-shadow: 3px 3px 4px #f4f4f5;
  -webkit-box-shadow: 3px 3px 4px #f4f4f5;
  box-shadow: 3px 3px 4px #f4f4f5;
  background: #fff;
  filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#F4F4F5');
}

.item .el-icon:last-child {
  float: right;
  margin: 0;
}

.bot_bottom_bot {
  width: 100%;
  height: 55px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bot_bottom_bot .el-button--mini {
  padding: 10px 35px;
}

.tip {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  text-align: center;
  font-size: 13px;
  color: rgb(144, 147, 153);
}
</style>
